package knowse.app.daos;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class TestStatusDAO {
	private final String TAG = "TestStatusDAO";
	private final String ERRORMSG = "Insertion Failed";
	
	protected static final String Table = "TestStatus"; 
	//primary key
	protected static final String TimeStamp = "_TimeStamp"; 
	protected static final String StartTime = "StartTime";
	protected static final String StartDate = "StartDate";
	protected static final String NumTests = "NumTests";
	protected static final String NumSkips = "NumSkips";
	protected static final String NumAssertions = "NumAssertions";	
	protected static final String NumFailures = "NumFailures";
	protected static final String NumErrors = "NumErrors";
	protected static final String ElapsedTime = "ElapsedTime";
	//Foreign key
	protected static final String ProjectName = "ProjectName";
	
	
	public TestStatusDAO(){
	}
	
	//insert methods to retrieve and update the database
	
	/*
	 * TODO
	 * Store TestStatusDataObject to the database
	 * 
	 */
	public void store(TestStatusDataObject tdo) throws Exception{

		ContentValues values= new ContentValues();
		values.put(TimeStamp, tdo.getTimeStamp().getTime());
		values.put(StartTime, tdo.getStartTime());
		values.put(StartDate, tdo.getStartDate());
		values.put(NumAssertions, tdo.getNumAssertions());
		values.put(NumErrors, tdo.getNumErrors());
		values.put(NumFailures, tdo.getNumFailures());
		values.put(NumSkips, tdo.getNumSkips());
		values.put(NumTests, tdo.getNumTests());
		values.put(ElapsedTime, tdo.getElapsedTime());
		values.put(ProjectName, "KnowSE");
		
		DBHelper dbHelper= new DBHelper();
		SQLiteDatabase db= dbHelper.getWritableDatabase();
		Long insertResult = db.insert(Table, null, values);
		if(insertResult < 0){
			throw new Exception(TAG + " " + ERRORMSG);
		}
		Log.i(TAG, insertResult.toString());
		db.close();
	}
	
	public void storeTrans(ArrayList<TestStatusDataObject> list) throws Exception{
		DBHelper dbHelper = new DBHelper();
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		for(TestStatusDataObject tdo : list){
			ContentValues values= new ContentValues();
			values.put(TimeStamp, tdo.getTimeStamp().getTime());
			values.put(StartTime, tdo.getStartTime());
			values.put(StartDate, tdo.getStartDate());
			values.put(NumAssertions, tdo.getNumAssertions());
			values.put(NumErrors, tdo.getNumErrors());
			values.put(NumFailures, tdo.getNumFailures());
			values.put(NumSkips, tdo.getNumSkips());
			values.put(NumTests, tdo.getNumTests());
			values.put(ElapsedTime, tdo.getElapsedTime());
			values.put(ProjectName, "KnowSE");
			Long insertResult = db.insert(Table, null, values);
			if(insertResult < 0){
				throw new Exception(TAG + " " + ERRORMSG);
			}
		}
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
	}
}
